package com.telsoft.hms.data;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.telsoft.hms.entity.ETT_OrderService;
import com.telsoft.hms.lib.admin.client.data.ClientDataPreprocessor;

/**
 * 
 * @author MANHNT Class phiếu yêu cầu dịch vụ
 */
public class OrderService_DATA extends ClientDataPreprocessor {	
	/**
	 * MANHNT
	 * @param strHospitalKey
	 *            - Mã bệnh viện
	 * @param iLanguageCode
	 *            - Mã ngôn ngữ
	 * @param lPatientId
	 *            - Mã bệnh nhân
	 * @return Danh sách các phiếu yêu cầu dịch vụ của bệnh nhân
	 */
	public List<ETT_OrderService> getAllOrderServiceByPatientId(
			String strHospitalKey, int iLanguageCode, long lPatientId)
			throws Exception {
		List<ETT_OrderService> lstOrderService = new ArrayList<>();
		String strQuery = "SELECT os.ORDER_SERVICE_ID, os.ORDER_SERVICE_CODE,TO_CHAR(os.CREATE_DATE, 'dd/mm/yyyy HH24:MI:SS') AS \"CREATE_DATE\","
				+ " os.SERVICE_CATEGORY_ID,"
				+ " os.PATIENT_ID,os.PATIENT_TYPE_CODE,os.AMOUNT,os.DEPT_ID,os.STATUS,"
				+ " svc.DISPLAY_VALUE AS \"SERVICE_CATEGORY_NAME\","
				+ " pt.DISPLAY_VALUE AS \"PATIENT_TYPE_NAME\","
				+ " d.DISPLAY_VALUE AS \"DEPARTMENT_NAME\""
				+ " FROM ORDER_SERVICE os"
				+ " JOIN SERVICE_CATEGORY_VALUES svc"
				+ " ON os.SERVICE_CATEGORY_ID = svc.SERVICE_CATEGORY_ID"
				+ " JOIN PATIENT_TYPE_VALUES pt"
				+ " ON os.PATIENT_TYPE_CODE = pt.PATIENT_TYPE_CODE"
				+ " JOIN DEPARTMENT_VALUES d"
				+ " ON os.DEPT_ID = d.DEPARTMENT_ID"
				+ " WHERE svc.LANGUAGE_CODE = ? AND pt.LANGUAGE_CODE = ? AND d.LANGUAGE_CODE = ? AND os.PATIENT_ID = ?";
		try {
			open(strHospitalKey);
			mStmt = mConnection.prepareStatement(strQuery);
			mStmt.setInt(1, iLanguageCode);
			mStmt.setInt(2, iLanguageCode);
			mStmt.setInt(3, iLanguageCode);
			mStmt.setLong(4, lPatientId);
			mRs = mStmt.executeQuery();
			while (mRs.next()) {
				ETT_OrderService objOrderService = new ETT_OrderService();
				objOrderService.setOrderServiceId(mRs.getLong("ORDER_SERVICE_ID"));
				objOrderService.setOrderServiceCode(mRs.getString("ORDER_SERVICE_CODE"));
				objOrderService.setCreateDate(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse(mRs.getString("CREATE_DATE")));
				objOrderService.setAmount(mRs.getDouble("AMOUNT"));
				objOrderService.setStatus(mRs.getInt("STATUS"));
				objOrderService.setServiceCategoryId(mRs.getLong("SERVICE_CATEGORY_ID"));
				objOrderService.setServiceCategoryName(mRs.getString("SERVICE_CATEGORY_NAME"));
				objOrderService.setPatientId(mRs.getLong("PATIENT_ID"));
				objOrderService.setPatientTypeCode(mRs.getString("PATIENT_TYPE_CODE"));
				objOrderService.setPatientTypeName(mRs.getString("PATIENT_TYPE_NAME"));
				objOrderService.setDepartmentId(mRs.getLong("DEPT_ID"));
				objOrderService.setDepartmentName(mRs.getString("DEPARTMENT_NAME"));
				lstOrderService.add(objOrderService);
			}
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			close(mConnection, mStmt, mRs);
		}
		return lstOrderService;
	}
}
